import SockJS from 'sockjs-client'
import Stomp from 'stompjs'
let stompClient = '';
let timer = '';

export const init = () => {
    connection()
    // 断开重连机制
    timer = setInterval(() => {
        try {
            stompClient.send('test')
        } catch (e) {
            console.log('连接中断:' + e)
            this.connection()
        }
    }, 10000)
}

export const connection = () => {
    debugger
    let socket = new SockJS('http://192.168.4.133:9003/notification');//后端提供协议字段
    stompClient = Stomp.over(socket);
    stompClient.connect(
        {},
        function connectCallback() {
            stompClient.subscribe('/topic/error-message-notification', (response) => {//后端提供订阅地址
                console.log(response)//接收后端response数据
            })
        },
        function errorCallBack(error) {
            console.log('连接失败:' + error);
        }
    )
}

export const disconnect = () => {
    if (stompClient) {
        stompClient.disconnect();
    }
}